package com.jzg.jcptadmin.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.BitmapUtils;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.view.ClipImageLayout;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TouXiangActivity extends BaseActivity {

    @Bind(R.id.clipImageLayout)
    ClipImageLayout clipImageLayout;
    @Bind(R.id.save)
    TextView save;

    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tou_xiang);
        ButterKnife.bind(this);
        path = getIntent().getStringExtra("path");
        // 有的系统返回的图片是旋转了，有的没有旋转，所以处理
        BitmapUtils utils = new BitmapUtils();
        Bitmap bitmap = null;
        int degreee = utils.readBitmapDegree(path);
        if (utils.saveImage(path, Environment.getExternalStorageDirectory() + "/JZGErShouChe/temp1.jpg"))
             bitmap = createBitmap(Environment.getExternalStorageDirectory() + "/JZGErShouChe/temp1.jpg");
        if (bitmap != null) {
            if (degreee == 0) {
                clipImageLayout.setImageBitmap(bitmap);
            } else {
                clipImageLayout.setImageBitmap(utils.rotateBitmap(degreee, bitmap));
            }
        } else {
            finish();
        }
    }

    @OnClick(R.id.save)
    public void onClick(View view) {

        if (view.getId() == R.id.save) {
            Bitmap bitmap = clipImageLayout.clip();
            saveBitmap(bitmap, path);
            Intent intent = new Intent();
            intent.putExtra(Constant.RESULT_PATH, path);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    private void saveBitmap(Bitmap bitmap, String path) {
        File f = new File(path);
        if (f.exists()) {
            f.delete();
        }

        FileOutputStream fOut = null;
        try {
            f.createNewFile();
            fOut = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG, 10, fOut);
            fOut.flush();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            try {
                if (fOut != null)
                    fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 创建图片
     *
     * @param path
     * @return
     */
    private Bitmap createBitmap(String path) {
        if (path == null) {
            return null;
        }

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 1;
        opts.inJustDecodeBounds = false;// 这里一定要将其设置回false，因为之前我们将其设置成了true
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        opts.inDither = false;
        opts.inPurgeable = true;
        FileInputStream is = null;
        Bitmap bitmap = null;
        try {
            is = new FileInputStream(path);
            bitmap = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                    is = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return bitmap;
    }


}
